import { Component, OnInit, ViewChild } from '@angular/core';
import { STComponent, STColumn, STChange, STRequestOptions, STData } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BasicTableComponent } from 'src/app/routes/commom';
import { TicketService } from '../../services/ticket.service';
import { ExpressDetailModalComponent } from './express-detail-modal/express-detail-modal.component';

@Component({
  selector: 'app-express-info',
  templateUrl: './express-info.component.html',
  styleUrls: ['../../../commom/less/commom-table.less']
})
export class ExpressInfoComponent extends BasicTableComponent {

  url = `/api/fcc/ficoExpressH/getListPage`;

  schema: SFSchema = {
    properties: {
      expressCode: {
        title: '快递单号',
        type: 'string',
        ui: {
          placeholder: '请输入'
        }
      },
      createTime: {
        title: '下单时间',
        type: 'string',
        ui: {
          widget: 'sl-from-to-search',
          format: 'yyyy-MM-dd'
        } as SFDateWidgetSchema
      }
    }
  };

  columns: STColumn[] = [
    { title: '', index: 'key', type: 'checkbox', width: 50 },
    {
      title: '序号',
      className: 'text-center',
      width: '60px',
      format: (item: STData, col: STColumn, idx: number) => {
        return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
      } 
    },
    { title: '快递单号', index: 'expressCode', width: 170 },
    { title: '快递公司', index: 'expresscompany', width: 120 },
    { title: '快递费用', index: 'description', width: 120 },
    {
      title: '发票数量',
      index: 'quantity',
      width: 120,
      type: 'link',
      click: (record: any) => this.showDetail(record.expressCode),
      acl: { ability: ['TICKET-EXPRESS-INFO-expressInvoices'] },
      className: 'text-right'
    },
    { title: '寄件人姓名', index: 'sname', width: 150 },
    { title: '寄件人电话', index: 'stel', width: 150 },
    {
      title: '寄件人地址',
      index: 'saddress',
      width: 150,
      format: item => `${item.sprovince}${item.scity}${item.scounty || ''}${item.saddress}`
    },
    { title: '收件人姓名', index: 'rname', width: 150 },
    { title: '收件人电话', index: 'rtel', width: 150 },
    {
      title: '收件人地址',
      index: 'raddress',
      width: 150,
      format: item => `${item.rprovince}${item.rcity}${item.rcounty || ''}${item.raddress}`
    },
    {
      title: '下单时间',
      index: 'createTime',
      type: 'date',
      width: 180
    }
  ];

  selectedRows: any[] = [];

  constructor(public service: TicketService, private nzModalService: NzModalService, public searchDrawerService: SearchDrawerService) {
    super(searchDrawerService);
  }

  ngOnInit(): void {}

  search() {
    this.st?.load(1);
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf?.value,
        createTime: {
          start: this.sf?.value.createTime?.[0] || '',
          end: this.sf?.value.createTime?.[1] || ''
        }
      });
    }
    return requestOptions;
  };

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
      case 'filter':
        this.st.load();
        break;
    }
  }

  showDetail(expressCode: any) {
    const modal = this.nzModalService.create({
      nzTitle: '发票明细',
      nzContent: ExpressDetailModalComponent,
      nzNoAnimation: true,
      nzWidth: 1100,
      nzComponentParams: { expressCode },
      nzOnOk: com => {
        console.log(com.selectedData);
      },
      nzOkText: '导出'
    });
    modal.afterClose.subscribe(res => {
      if (res) {
        this.st.load();
      }
    });
  }

  printOrder() {
    if (this.selectedRows?.length <= 0) {
      this.service.msgSrv.warning('请选择快递单');
      return;
    }
    this.nzModalService.warning({
      nzTitle: '确认打印面单所选快递单？',
      nzClosable: false,
      nzCancelText: '取消',
      nzOnOk: () => {
        this.service
          .request(
            this.service.$api_get_print_pdf,
            this.selectedRows.map(item => item.expressCode)
          )
          .subscribe(res => {
            if (res?.pdfUrl) {
              this.service.reviewPDF(res.pdfUrl);
            } else {
              this.service.msgSrv.warning('下载失败');
            }
          });
      }
    });
  }
}
